package com.tz.crm.service.impl;


import com.tz.crm.dao.AnnouncementDao;
import com.tz.crm.entity.Announcement;
import com.tz.crm.service.AnnouncementService;
import com.tz.crm.utils.Pager;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * AnnouncementServiceImpl 实现类
 * 关于公告的一些增删改查操作
 */
@Service
@Transactional
@SuppressWarnings("unchecked")
public class AnnouncementServiceImpl extends BaseServiceImpl<Announcement, String> implements AnnouncementService {

    @Autowired
    private AnnouncementDao announcementDao;

    /**
     * 有参构造
     *
     * @param announcementDao 注入的 AnnouncementDao 组件
     */
    @Autowired
    public AnnouncementServiceImpl(AnnouncementDao announcementDao) {
        this.announcementDao = announcementDao;
        super.setBaseDao(announcementDao);
    }

    @Override
    public boolean validUpdateAnnouncement(String title, String id) {
        Announcement ac = announcementDao.getByTitle(title);
        if (ac == null) {
            return true;
        } else {
            return ac.getId().equals(id);
        }
    }

    @Override
    public Pager<Announcement> getByPager(Pager pager, Announcement announcement) {
        if (pager == null) {
            pager = new Pager();
        }
        DetachedCriteria dc = DetachedCriteria
                .forClass(Announcement.class);
        pager = announcementDao.findByPager(pager, dc, Order.desc("modifyDate"));
        return pager;
    }

}
